/*
 * Copyright (c) 2011, Michael van der Westhuizen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *    - Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    - Redistributions in binary form must reproduce the above
 *      copyright notice, this list of conditions and the following
 *      disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef PYXMLSEC1_PARSER_C_MCVDW_20110104
#define PYXMLSEC1_PARSER_C_MCVDW_20110104

#include "pyxmlsec.h"

#include <xmlsec/parser.h>



static PyObject * xmlsec1_xmlSecParseFile(PyObject * self, PyObject * args, PyObject * kwds)
{
    char * filename;
    xmlDocPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"filename", NULL};

    filename = NULL;
    ptr = NULL;
    ret= NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &filename))
    {
        return NULL;
    }

    if (NULL == (ptr = xmlSecParseFile(filename)))
    {
        ENSURE_EXCEPTION_SET(xmlSecParseFile);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlDocPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlDocPtr);
        xmlFreeDoc(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecParseMemory(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    int recovery;
    xmlSecByte * buffer;
    xmlSecSize size;
    xmlDocPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"buffer", "recovery", NULL};

    arg1 = NULL;
    recovery = 0;
    buffer = NULL;
    size = 0;
    ptr = NULL;
    ret= NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oi", kwlist, &arg1, &recovery))
    {
        return NULL;
    }

    if (0 != xmlsec1_ByteArrayOrBytesToXmlSecBytes(arg1, &buffer, &size))
    {
        ENSURE_EXCEPTION_SET(xmlsec1_ByteArrayOrBytesToXmlSecBytes);
        return NULL;
    }

    if (NULL == (ptr = xmlSecParseMemory(buffer, size, recovery)))
    {
        ENSURE_EXCEPTION_SET(xmlSecParseMemory);
        PyMem_Del(buffer);
        return NULL;
    }

    PyMem_Del(buffer);

    if (NULL == (ret = xmlsec1mod_wrap_xmlDocPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlDocPtr);
        xmlFreeDoc(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecParseMemoryExt(PyObject * self, PyObject * args, PyObject * kwds)
{
    PyObject * arg1;
    PyObject * arg2;
    PyObject * arg3;
    xmlSecByte * prefix;
    xmlSecByte * buffer;
    xmlSecByte * postfix;
    xmlSecSize prefixSize;
    xmlSecSize bufferSize;
    xmlSecSize postfixSize;
    xmlDocPtr ptr;
    PyObject * ret;

    static char * kwlist[] = {"prefix", "buffer", "postfix", NULL};

    arg1 = arg2 = arg3 = NULL;
    prefix = buffer = postfix = NULL;
    prefixSize = bufferSize = postfixSize = 0;
    ptr = NULL;
    ret= NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &arg1, &arg2, &arg3))
    {
        return NULL;
    }

    if (NULL != arg1 && Py_None != arg1)
    {
        if (0 != xmlsec1_ByteArrayOrBytesToXmlSecBytes(arg1, &prefix, &prefixSize))
        {
            ENSURE_EXCEPTION_SET(xmlsec1_ByteArrayOrBytesToXmlSecBytes);
            return NULL;
        }
    }

    if (NULL != arg2 && Py_None != arg2)
    {
        if (0 != xmlsec1_ByteArrayOrBytesToXmlSecBytes(arg2, &buffer, &bufferSize))
        {
            ENSURE_EXCEPTION_SET(xmlsec1_ByteArrayOrBytesToXmlSecBytes);

            if (NULL != prefix)
            {
                PyMem_Del(prefix);
            }

            return NULL;
        }
    }

    if (NULL != arg3 && Py_None != arg3)
    {
        if (0 != xmlsec1_ByteArrayOrBytesToXmlSecBytes(arg3, &postfix, &postfixSize))
        {
            ENSURE_EXCEPTION_SET(xmlsec1_ByteArrayOrBytesToXmlSecBytes);

            if (NULL != prefix)
            {
                PyMem_Del(prefix);
            }

            if (NULL != buffer)
            {
                PyMem_Del(buffer);
            }

            return NULL;
        }
    }

    if (NULL == (ptr = xmlSecParseMemoryExt(prefix, prefixSize, buffer, bufferSize, postfix, postfixSize)))
    {
        ENSURE_EXCEPTION_SET(xmlSecParseMemoryExt);

        if (NULL != prefix)
        {
            PyMem_Del(prefix);
        }

        if (NULL != buffer)
        {
            PyMem_Del(buffer);
        }

        if (NULL != postfix)
        {
            PyMem_Del(postfix);
        }

        return NULL;
    }
    
    if (NULL != prefix)
    {
        PyMem_Del(prefix);
    }

    if (NULL != buffer)
    {
        PyMem_Del(buffer);
    }

    if (NULL != postfix)
    {
        PyMem_Del(postfix);
    }
    
    if (NULL == (ret = xmlsec1mod_wrap_xmlDocPtr(ptr)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlDocPtr);
        xmlFreeDoc(ptr);
        return NULL;
    }

    return ret;
}



static PyObject * xmlsec1_xmlSecTransformXmlParserGetKlass(PyObject * self, PyObject * args)
{
    xmlSecTransformId klass;
    PyObject * ret;

    if (NULL == (klass = xmlSecTransformXmlParserGetKlass()))
    {
        ENSURE_EXCEPTION_SET(xmlSecTransformXmlParserGetKlass);
        return NULL;
    }

    if (NULL == (ret = xmlsec1mod_wrap_xmlSecTransformId(klass)))
    {
        ENSURE_EXCEPTION_SET(xmlsec1mod_wrap_xmlSecTransformId);
        return NULL;
    }

    return ret;
}

#endif
